home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / editor / snmp_0_1.zip / snmp-0.1 / aka / snmp / ByteBuffer.java < prev    next >
Text File  |  1997-06-09  |  5KB  |  230 lines

  1. /*
  2. Snmp Library
  3. Copyright (C) 1997 Alex Kowalenko Associates Pty Ltd. All rights reserved.
  4.  
  5. This software maybe be free distributed, any any form, without fee, 
  6. but may not be modified in any way without express permission of 
  7. the directors of Alex Kowalenko Associates Pty Ltd. 
  8.  
  9. Alex Kowalenko Associates Pty Ltd makes no representations or
  10. warranties about the suitabililty of the software, not even the
  11. implied warranty of merchantability or fitness for any particular
  12. purpose.    
  13. */
  14.  
  15. package aka.snmp;
  16.  
  17. /**
  18.  * This class is similar to StringBuffer, but handles arrays of 8-bit
  19.  * characters.
  20.  * @version     $Id: ByteBuffer.java,v 1.6 1997/05/18 07:34:11 alex Exp $
  21.  * @author      Alex Kowalenko
  22.  */
  23.  
  24. public class ByteBuffer {
  25.  
  26.   private int _start; // Start of array  
  27.   private int _end;   // Next avaliable position 
  28.   private byte _array[];
  29.     
  30.   public static int STARTSIZE = 32;
  31.     
  32. /** 
  33.  * Default constructor 
  34.  */
  35.     
  36.   public ByteBuffer() {
  37.       _array = new byte[STARTSIZE];
  38.       _start = _end = 0;
  39.   };
  40.     
  41. /**
  42.  * Constructor from a byte[], copys a subarray into the object
  43.  * @param array the array
  44.  * @param length length of the subarray
  45.  */  
  46.   
  47.   public ByteBuffer(byte array[], int length) {
  48.       _array = new byte[ length > STARTSIZE ? length : STARTSIZE];
  49.       System.arraycopy(array, 0, _array, 0, length); 
  50.       _start = 0;
  51.       _end = length;
  52.   };
  53.  
  54. /**
  55.  * append byte b to the end of the buffer.
  56.  */
  57.  
  58.   public ByteBuffer append(byte b) {
  59.       if(_end >= _array.length)
  60.       selfExtend(1);
  61.       _array[_end] = b;
  62.       _end++;
  63.       return this;
  64.   };
  65.  
  66. /**
  67.  * append the array to the end of the buffer.
  68.  */
  69.  
  70.   public ByteBuffer append(byte array[]) {
  71.       if(_end + array.length >= _array.length)
  72.       selfExtend(array.length);
  73.       System.arraycopy(array, 0, _array, _end, array.length); 
  74.       _end += array.length;
  75.       return this;
  76.   };
  77.  
  78. /**
  79.  * append the subarray to the end of the buffer.
  80.  */
  81.  
  82.   public ByteBuffer append(byte array[], int size) {
  83.       if(_end + size >= _array.length)
  84.       selfExtend(size);
  85.       System.arraycopy(array, 0, _array, _end, size); 
  86.       _end += size;
  87.       return this;
  88.   };
  89.  
  90. /**
  91.  * append a ByteBuffer to the end of the buffer.
  92.  */
  93.  
  94.   public ByteBuffer append(ByteBuffer buffer) {
  95.       if(_end + buffer._end - buffer._start >= _array.length)
  96.       selfExtend(buffer._end - buffer._start);
  97.       System.arraycopy(buffer._array, buffer._start, 
  98.                _array, _end, buffer._end - buffer._start); 
  99.       _end += buffer._end - buffer._start ;
  100.       return this;
  101.   };
  102.  
  103. /**
  104.  * Append a string to the end of the array, 16-bit characters truncated
  105.  * to 8-bit characters
  106.  */
  107.  
  108.   public ByteBuffer append(String str) {
  109.       if(_end + str.length() >= _array.length)
  110.       selfExtend(str.length());
  111.       byte tmp[] = str.getBytes();
  112.       System.arraycopy(tmp, 0, _array, _end, tmp.length); 
  113.       _end += tmp.length;
  114.       return this;
  115.   };
  116.  
  117. /**
  118.  * return the byte[] array
  119.  */
  120.  
  121.   public byte[] array() {
  122.       if(_start == 0 ) {
  123.       return _array;
  124.       } else {
  125.       byte copy[] = new byte[_end - _start];
  126.       System.arraycopy(_array, _start, copy, 0, _end - _start);
  127.       return copy;
  128.       }
  129.   };
  130.  
  131. /**
  132.  * byte at an index
  133.  */
  134.  
  135.   public byte byteAt(int index) throws ArrayIndexOutOfBoundsException {
  136.       return _array[_start + index];
  137.   };
  138.  
  139.  
  140. /** 
  141.  * implementation of equals
  142.  */
  143.  
  144.   public boolean equals(byte array[]) {
  145.       for(int i = 0; i < ( size() < array.length ? size() : array.length); i++)
  146.       if( _array[_start + i] != array[i])
  147.           return false;
  148.       return true;
  149.   };
  150.  
  151. /**
  152.  * size of the array
  153.  */
  154.  
  155.   public int size() {
  156.       return _end - _start;
  157.   };
  158.  
  159. /**
  160.  * sets the byte at an index to b
  161.  */
  162.  
  163.   public void setByteAt(int index, byte b) 
  164.       throws ArrayIndexOutOfBoundsException {
  165.       _array[_start + index] = b;
  166.       return;
  167.   };
  168.  
  169. /**
  170.  * remove the first length
  171.  */
  172.  
  173.   public void removeBeginning(int length) {
  174.       if(length > (_end - _start)) { 
  175.       _start = _end = 0;      
  176.       }
  177.       else {
  178.       _start += length;
  179.       };
  180.   };
  181.  
  182. /**
  183.  * truncate the array to this length
  184.  */
  185.  
  186.     void truncate(int length) {
  187.     if(_end - _start > length)
  188.         _end = _start + length;
  189.     return;
  190.     }
  191.  
  192. /**
  193.  * return the string representation of the array
  194.  */
  195.  
  196.   public String toString() {
  197.       return new String(_array, _start, _end - _start);
  198.   };
  199.  
  200.     /**
  201.      * return the string representation of the array to this length
  202.      */
  203.  
  204.   public String toString(int len) {
  205.       return new String(_array, 
  206.             _start,
  207.             ((_end - _start) < len ? _end - _start : len));
  208.   };
  209.  
  210.  
  211.   private void selfExtend(int size) {
  212.       byte newArray[] = new byte[ size > STARTSIZE 
  213.                 ? _array.length + size + STARTSIZE 
  214.                 : _array.length + STARTSIZE];
  215.       System.arraycopy(_array, _start, newArray, 0, _end - _start);
  216.       _array = newArray;
  217.   };
  218.   
  219.  
  220. /**
  221.  * Test harness
  222.  */
  223.  
  224.   public static void main(String args[]) {
  225.       byte a[] = { 1, 2, 3};
  226.       ByteBuffer b = new ByteBuffer(a, a.length);
  227.       b.removeBeginning(1);
  228.   };
  229. };
  230.